package gwtadmin.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;

public class TableOfServices extends Table implements ClickHandler {
	
	private IWsdlManager wsdlMan = null;
	private Image imageWait = new Image("rsrc/wait30.gif");
	
	private static final String STR_DOWNLOAD = "Download";
	private static final String STR_REMOVE = "Remove";
	
	private int svcNoServices = -1;
	private int svcWait = -1;
	private int colFileName = -1;
	
	public TableOfServices(IWsdlManager wsdlManager) {
		super();
		
		wsdlMan = wsdlManager;
		
		addColumn("№");
		colFileName = addColumn("File Name");
		addColumn("Download");
		addColumn("Remove");
		
		svcNoServices = addSvcRow("No services");
		svcWait = addSvcRow(imageWait);
		
		showSvc(svcNoServices, false);
		showSvc(svcWait, false);
	}
	
	public void addService(String fileName) {
		int row = addItemRow();
		
		Hyperlink hlFileName = new Hyperlink(fileName, null);
		hlFileName.addClickHandler(this);

		Button btnDownload = new Button(STR_DOWNLOAD, this);
		btnDownload.setTitle(fileName);

		Button btnRemove = new Button(STR_REMOVE, this);
		btnRemove.setTitle(fileName);

		setItem(row, 0, String.valueOf(getItemCount()));
		setItem(row, 1, hlFileName);
		setItem(row, 2, btnDownload);
		setItem(row, 3, btnRemove);
		
		showAllServices(true);
	}
	
	public void removeServices() {

		clearItems();
		showNoServices(true);
	}
	
	public void setServices(String[] svcs) {
		if (svcs == null)
			return;

		removeServices();
		for (String svc : svcs)
			addService(svc);
	}
	
	public void showNoServices(boolean show) {
		showSvc(svcNoServices, show);
		
		if (show) {
			showWait(false);
			showAllServices(false);
		}
	}
	
	public void showAllServices(boolean show) {
		showItems(show);
		
		if (show) {
			showNoServices(false);
			showWait(false);
		}
	}
	
	public void showWait(boolean show) {
		imageWait.setVisible(show);
		showSvc(svcWait, show);
		
		if (show) {
			showAllServices(false);
			showNoServices(false);
		}
	}
	
	public void setFilter(String filter) {
		boolean res = setFilter(colFileName, filter);
		showNoServices(!res);
	}
	
	public String getWsdlName(int serviceIndex) {
		return getText(serviceIndex, colFileName);
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource().getClass() == Hyperlink.class) {
			Hyperlink hl = (Hyperlink) event.getSource();
			wsdlMan.goEditWsdl(hl.getText());
		} else if (event.getSource().getClass() == Button.class) {
			Button btn = (Button) event.getSource();

			if (btn.getText().equals(STR_REMOVE)) {
				wsdlMan.removeWsdl(btn.getTitle());
			} else if (btn.getText().equals(STR_DOWNLOAD)) {
				wsdlMan.downloadWsdl(btn.getTitle());
			}
		}
	}
}
